package v2.parser

import scala.collection.mutable.Map

/**
 * @author bweber
 * @version $Rev$, $Date$
 */

class Type private (val sname:Symbol,var attrdefs:List[Attr]) extends Value with Attributed {
  override def toString() = {
    "type named " + sname.name + " with attrs " + attrdefs
  }
  
  def name():String = {
    sname.name
  }
  
  def getType() = {
    this
  }
  
  def isOfType(_type:Type):Boolean = {
    this == _type
  }
  
}

object Type {

  def apply(name:String,attrs:Attr*):Type = {
    if ( types.keys.contains(Symbol(name))) {
    }
    else {
      types.put(Symbol(name),new Type(Symbol(name),attrs.toList))
    }
    types.get(Symbol(name)) match { 
      case Some(x) => x
    }
  }
  
  def apply(name:String,attrdefs:List[Attr]):Type = {
    if ( types.keys.contains(Symbol(name))) {
    }
    else {
      types.put(Symbol(name),new Type(Symbol(name),attrdefs))
    }
    types.get(Symbol(name)) match { 
      case Some(x) => x
    }
  }
  
  def apply(typeRef:TypeRef):Type = {
    apply(typeRef.name)
  }
  
  val types: Map[Symbol, Type] = Map[Symbol, Type]()
  types.put('type, new Type('type,List[Attr]()))
  
  def listTypeNames():Iterator[String] = {
    types.keys.map { k => k.name }
  }
  
  def listTypes():Bunch = {
    val bunch = Bunch(Type.typeRef("type"),List[Slot]())
    for ( _type <- types.values ) {
        bunch.addAttr(_type.name,_type)
    }
    bunch
  }
  
  def isType(name:String):Boolean = {
    types.keys.contains(Symbol(name))
  }
  
  def typeRef(name:String):TypeRef = {
    if ( types.keys.contains(Symbol(name)) ) {
      return TypeRef(name)
    }
    throw new RuntimeException("No such type: " + name)
  }
}